home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_gnome-python.idb / usr / freeware / lib / python1.5 / site-packages / pyglade / style.py.z / style.py
Encoding:
Python Source  |  1999-07-16  |  2.4 KB  |  78 lines

  1. """This file creates a GTK rc file to work with a widget tree created from
  2. the XML output of GLADE.
  3.  
  4. It outputs both the style definitions, and links to the names of the widgets
  5. that use it.  The style namesare of the form "PyGLADE_<name>_style".  Then
  6. there is a number of "widget <path> style PyGLADE_<name>_style" lines that
  7. bind the styles to particular widgets.
  8. """
  9. import string
  10.  
  11. # Get the fastest implementation of StringIO ...
  12. try:
  13.     from cStringIO import StringIO
  14. except ImportError:
  15.     from StringIO import StringIO
  16.  
  17. error = 'pyglade.style.error'
  18.  
  19. def convert_colour(str):
  20.     r, g, b = map(string.atoi, string.split(str, ','))
  21.     return "{ %.3f, %.3f, %.3f }" % (r/255.0, g/255.0, b/255.0)
  22.  
  23. def output_widget_names(fp, widget, parent):
  24.     parent = parent + '.' + widget.name
  25.     if widget.has_key('style_name'):
  26.         fp.write('widget "%s" style "PyGLADE_%s_style"\n' %
  27.              (parent[1:], widget.style_name))
  28.     if not widget.has_key('widget'):
  29.         return
  30.     children = widget.widget
  31.     if type(children) != type(()): children = (children,)
  32.     for child in children:
  33.         output_widget_names(fp, child, parent)
  34.  
  35. def write_stream(tree, fp):
  36.     if tree.has_key('gtk-interface'):
  37.         # let the user off if they just used the top of the parse tree
  38.         # instead of the GTK-Interface node
  39.         tree = tree['gtk-interface']
  40.     if tree.tag != 'gtk-interface':
  41.         raise error, "Not at the top node of the GLADE output"
  42.  
  43.     styles = tree.style
  44.     if type(styles) != type(()): styles = (styles,)
  45.     for style in styles:
  46.         name = "PyGLADE_" + style.style_name + "_style"
  47.         fp.write('style "%s"\n{\n' % (name,))
  48.         if style.has_key('style_font'):
  49.             fp.write('  font = "%s"\n' % (style.style_font,))
  50.         for state in ('NORMAL', 'ACTIVE', 'PRELIGHT',
  51.                   'SELECTED', 'INSENSITIVE'):
  52.             for colour in 'fg', 'bg', 'text', 'base':
  53.                 key = colour + ':' + string.lower(state)
  54.                 if style.has_key(key):
  55.                     cstr = convert_colour(style[key])
  56.                     fp.write("  %s[%s] = %s\n" % (
  57.                         colour, string.upper(state),
  58.                         cstr))
  59.             key = "bg_pixmap:" + state
  60.             if style.has_key(key):
  61.                 fp.write("  bg_pixmap[%s] = \"%s\"\n" % (
  62.                     state, style[key]))
  63.         fp.write("}\n\n")
  64.     if not tree.has_key('widget'):
  65.         return
  66.     children = tree.widget
  67.     if type(children) != type(()): children = (children,)
  68.     for child in children:
  69.         output_widget_names(fp, child, '')
  70.  
  71. def write(tree, fname):
  72.     write_stream(tree, open(fname, "w"))
  73.  
  74. def as_string(tree):
  75.     fp = StringIO()
  76.     write_stream(tree, fp)
  77.     return fp.getvalue()
  78.